package edu.geo.service.impl;

import java.util.List;

import edu.geo.containers.geom.impl.LineSegment;
import edu.geo.containers.geom.impl.Point;
import edu.geo.containers.geom.impl.Polygon;
import edu.geo.containers.geomdomain.GeometricDomain;
import edu.geo.containers.geomdomain.GeometricDomainFault;
import edu.geo.containers.geomdomain.Grid;
import edu.geo.dao.IGeometricDomainDao;
import edu.geo.exception.GeoInfoException;
import edu.geo.geomdomain.GeometricDomainFixer;
import edu.geo.geomdomain.GeometricDomainValidator;
import edu.geo.service.IGeometricDomainServiceFacade;
import edu.geo.spaghetti.SpaghettiRepresentation;
import edu.geo.spaghetti.SpaghettiTransformer;

/**
 * Implementa los métodos que permiten crear, eliminar, modificar, validar y
 * recuperar un dominio geométrico o un conjunto de dominios geométricos.
 * 
 * @author alejandro.sanchez
 * 
 */
public class GeometricDomainServiceFacade implements
		IGeometricDomainServiceFacade {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2209685438641814486L;
	
	private IGeometricDomainDao geometricDomainDao;
	
	
	public GeometricDomainServiceFacade(IGeometricDomainDao geometricDomainDao)
	{
		this.geometricDomainDao = geometricDomainDao;
	}
	

	@Override
	public int save(GeometricDomain geomDomain) throws GeoInfoException {
		return geometricDomainDao.insertGeometricDomain( geomDomain );
	}

	@Override
	public void delete(int geomDomainId) throws GeoInfoException {
		geometricDomainDao.deleteGeometricDomainDao(geomDomainId);
	}

	@Override
	public void addPoint(int geomDomainId, Point point) throws GeoInfoException {
		geometricDomainDao.addPoint(geomDomainId, point);
	}

	@Override
	public void addLineSegment(int geomDomainId, LineSegment lineSegment)
			throws GeoInfoException {
		geometricDomainDao.addLineSegment(geomDomainId, lineSegment);
	}

	@Override
	public void deletePoint(int geomDomainId, Point point)
			throws GeoInfoException {
		geometricDomainDao.deletePoint(geomDomainId, point);				
	}

	@Override
	public void deleteLineSegment(int geomDomainId, LineSegment lineSegment)
			throws GeoInfoException {
		geometricDomainDao.deleteLineSegment(geomDomainId, lineSegment);
	}	

	@Override
	public List<GeometricDomain> retrieveAll() throws GeoInfoException {
		return geometricDomainDao.retrieveGeometricDomains();
	}

	@Override
	public GeometricDomain retrieve(int id) throws GeoInfoException {
		return geometricDomainDao.retrieveGeometricDomain(id);
	}
	
	@Override
	public int verifyPointExistence(Point point) throws GeoInfoException {
		return geometricDomainDao.getGeometryDao().verifyPointExistence(point);
	}


	@Override
	public int verifyLineSegmentExistence(LineSegment lineSegment)
			throws GeoInfoException {
		return geometricDomainDao.getGeometryDao().verifyLineSegmentExistenceByGeom(lineSegment);
	}

	
	@Override
	public List<GeometricDomainFault> validate(GeometricDomain geomDomain)
			throws GeoInfoException {
		return GeometricDomainValidator.validate(geomDomain);
	}

	@Override
	public GeometricDomain adjust(GeometricDomain geomDomain, boolean store)
			throws GeoInfoException {
		GeometricDomain fixedGeomDomain = GeometricDomainFixer.adjust(geomDomain);
		if (store)
		{
			geometricDomainDao.insertValidGeometricDomain(geomDomain.getId(), fixedGeomDomain);
		}
		return fixedGeomDomain;		
	}


	@Override
	public boolean modifyGeometriesDomain(int geomDomainId, List<Point> points,
			List<LineSegment> lineSegments) throws GeoInfoException {
		return geometricDomainDao.modifyGeometriesDomain(geomDomainId, points, lineSegments);
	}

	@Override
	public Grid retrieveGridGeomDomain(int geomDomainId)
			throws GeoInfoException {
		return geometricDomainDao.getGridDao().retrieveGridGeomDomain(geomDomainId);
	}
	
	@Override
	public void deleteFeature(int geomDomainId, int featureId) throws GeoInfoException {		
		geometricDomainDao.deleteFeature(geomDomainId, featureId);
	}


	@Override
	public List<GeometricDomainFault> validate(int geomDomainId)
			throws GeoInfoException {
		GeometricDomain geomDomain = retrieve(geomDomainId);		
		return GeometricDomainValidator.validate(geomDomain);
	}
	
	@Override
	public SpaghettiRepresentation asSpaghettiRepresentation(
			GeometricDomain geometricDomain) {		
		return SpaghettiTransformer.asSpaghettiRepresentation(geometricDomain);
	}


	
}
